﻿using HomagChina.CCHCS.Views;
using HomagGroup.Base.UI;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Threading;
using static HomagChina.CCHCS.Utility;

namespace HomagChina.CCHCS.ViewModels
{
    public class BoardLibraryViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private CommonHandle commonHandle;
        private DeleteInfoHandle Handle_Delete;
        private SearchInfoHandle Handle_Search;

        public RelayCommand EditCommand { get; }
        public RelayCommand UnLoadedCommand { get; }
        public RelayCommand LoadedCommand { get; }

        #endregion Handle

        #region 属性

        private string reference;

        public string Reference
        {
            get => reference;
            set { reference = value; Query(); OnPropertyChanged(); }
        }

        private bool _SelectedState = true;

        public bool SelectedState
        {
            get => _SelectedState;
            set { _SelectedState = value; OnPropertyChanged(); }
        }

        private ObservableCollection<object> _RemoveList = new ObservableCollection<object>();

        public ObservableCollection<object> RemoveList
        {
            get => _RemoveList;
            set
            {
                _RemoveList = value;
                OnPropertyChanged();
            }
        }

        private ObservableCollection<BoardInfoModel> _List_BoardInfoModel;

        public ObservableCollection<BoardInfoModel> List_BoardInfoModel
        {
            get => _List_BoardInfoModel;
            set { _List_BoardInfoModel = value; OnPropertyChanged(); }
        }

        public event EventHandler RequestScrollPositionSave;

        public event EventHandler RequestScrollPositionRestore;

        private DispatcherTimer UpdateSessionTimer;

        #endregion 属性

        #region 构造函数

        public BoardLibraryViewModel()
        {
            commonHandle = new CommonHandle();
            Handle_Delete = new DeleteInfoHandle();
            Handle_Search = new SearchInfoHandle();
            EditCommand = new RelayCommand(HandleEditCommand);
            Query();
        }

        /// <summary>
        /// Unloaded
        ///
        /// </summary>
        private void ViewUnloadedCommand()
        {
            UpdateSessionTimer.Stop();
        }

        #endregion 构造函数

        #region 函数

        public void Query()
        {
            RequestScrollPositionSave?.Invoke(this, EventArgs.Empty);
            List_BoardInfoModel = Handle_Search.GetBoardsInfo(false, Reference);
            RequestScrollPositionRestore?.Invoke(this, EventArgs.Empty);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        public void BoardAdd()
        {
            BoardEditingViewModel boardEditingViewModel = new BoardEditingViewModel(null, false);
            UIAdjustments.CurrentThreadDialogManager.ShowDialog<BoardEditing>(boardEditingViewModel, null);
            Query();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        public void HandleEditCommand(object SelectItem)
        {
            BoardEditingViewModel boardEditingViewModel = new BoardEditingViewModel(SelectItem as BoardInfoModel, true);
            UIAdjustments.CurrentThreadDialogManager.ShowDialog<BoardEditing>(boardEditingViewModel, null);
            Query();
        }

        /// <summary>
        /// 处理删除命令
        /// </summary>
        /// <param name="obj"></param>
        public void Delete()
        {
            var result = HomagGroup.Base.UI.Windows.MessageBox.Show(
                Application.Current.MainWindow,
                GetLocalText("Are you sure you want to delete this Information"),
                GetLocalText("Warning"),
                GetLocalText("Yes"),
                GetLocalText("Cancel"),
                MessageBoxImage.Question,
                MessageBoxResult.Yes);
            if (result == MessageBoxResult.Yes)
            {
                List<int> list = new List<int>();
                foreach (var item in RemoveList)
                {
                    list.Add((item as BoardInfoModel).Id);
                }
                Handle_Delete.DeleteBoardsInfos(list);
                Query();
            }
        }

        /// <summary>
        ///
        /// </summary>
        public void HandleOpenMprFile()
        {
            try
            {
                BoardImportViewModel boardimportViewModel = new BoardImportViewModel();
                UIAdjustments.CurrentThreadDialogManager.ShowDialog<BoardImport>(boardimportViewModel, null);
                Query();
            }
            catch (Exception ex)
            {
                //SerilogHandle.Logger_DealWithCsv.Error(ex.Message);
            }
        }
    }

    #endregion 函数
}